use axum::{http::status::StatusCode, routing::get, Router};
use tokio::{
    self,
    sync::{mpsc, oneshot},
};

mod grpc;
mod postgres_client;
mod types;

#[tokio::main]
async fn main() {
    // Channel with capacity 1 to avoid duplicate threads collisions, so need not to use locks
    // let (tx_redis, rx_redis) = mpsc::channel::<(
    //     types::channel::Request,
    //     oneshot::Sender<types::channel::Response>,
    // )>(1);

    let (tx_to_db, rx_by_db) = mpsc::channel::<(
        types::channel::Request,
        oneshot::Sender<types::channel::Response>,
    )>(1);

    let _ = tokio::join!(
        k8s_healthy_serve_start(),
        grpc::server::start(tx_to_db),
        postgres_client::client::start(rx_by_db),
    );
}

async fn k8s_healthy_serve_start() {
    let app = Router::new()
        .route("/ready", get(|| async { return StatusCode::OK }))
        .route("/alive", get(|| async { return StatusCode::OK }));

    // Start the server on localhost:9000.
    let listener = tokio::net::TcpListener::bind("0.0.0.0:9000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
